/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-08-12     admin       the first version
 */
#include "rtthread.h"
#include "stdlib.h"

#include "sy_bc26.h"
#include "sy_queue.h"
#include "sy_http_client.h"

static int Bc26_Httpconnect(unsigned char *url,unsigned int port);
static int Bc26_SendSocket(unsigned char *msg,int len);

int Bc26_HttpInit()
{
    Bc26_Httpconnect((unsigned char*)"47.113.94.229",80);
    char cmd[]="GET http://47.113.94.229/STM32IAPUP/binfile/20210319165234.bin\r\n";
    //char cmd[]="GET http://47.113.94.229/TEST/index.html\r\n";
    int ret=Bc26_SendSocket((unsigned char*)cmd,rt_strlen(cmd));
    if(ret!=0)
    {
        rt_kprintf("bc26 send socket error:%d\n",ret);
    }
    return 0;
}

static int Bc26_Httpconnect(unsigned char *url,unsigned int port)
{
    unsigned char cmd[128];
    unsigned char rec_buf[64];

    if(url==RT_NULL)
    {
        return -1;
    }
    rt_sprintf((char*)cmd,"AT+QICLOSE=1\r\n");
    Bc26_SendCmd(cmd);
    if(Bc26_WaitForRet((unsigned char*)rec_buf,sizeof(rec_buf),500)!=RT_NULL)
    {
        return -2;
    }
    rt_sprintf((char*)cmd,"AT+QIOPEN=0,1,\"TCP\",\"%s\",%d\r\n",url,port);
    Bc26_SendCmd(cmd);
    if(Bc26_WaitForRet((unsigned char*)rec_buf,sizeof(rec_buf),500)!=RT_NULL)
    {
        return -3;
    }
    int count=50;
    while(count--)
    {
        if(bc26_state.tcp_connect==1)
        {
            break;
        }
        rt_thread_mdelay(100);
    }
    if(count<=0)
    {
        return -4;
    }

    return 0;
}
static int Bc26_SendSocket(unsigned char *msg,int len)
{
    unsigned char cmd[128];
    unsigned char rec_buf[64];

    if(msg==RT_NULL||len<=0)
    {
        return -1;
    }
    rt_sprintf((char*)cmd,"AT+QISEND=1,%d\r\n",len);
    Bc26_SendCmd(cmd);
    if(Bc26_WaitForRet(rec_buf,sizeof(rec_buf),1000)!=BC26_RETREADY)
    {
        return -2;
    }
    rt_sprintf((char*)cmd,"%s",msg);
    Bc26_SendCmd(msg);
    if(Bc26_WaitForRet(rec_buf,sizeof(rec_buf),2000)!=BC26_RETOK)
    {
        return -3;
    }
    return 0;
}
void Bc26_HttpRecCallback(unsigned char *msg,unsigned int size)
{
    if(msg==RT_NULL||size==0)
    {
        return;
    }
    unsigned char *p=(unsigned char*)rt_strstr((const char*)msg,"+QIURC: \"recv\",1,");
    if(p==RT_NULL)
    {
        return;
    }
    p+=17;
    unsigned char *p1=(unsigned char*)rt_strstr((const char*)p,",");
    if(p1==RT_NULL)
    {
        rt_kprintf("msg , null\n");
        return;
    }
    char msg_len[5];
    if((p1-p)>sizeof(msg_len))
    {
        rt_kprintf("msg len error\n");
        return;
    }
    rt_memcpy(msg_len,p, p1-p);
    msg_len[p1-p]='\0';
    p1+=2;
    p1[atoi(msg_len)]='\0';
    Http_Dataanalysis(p1,atoi(msg_len));
    //rt_kprintf("len:%d msg:%s\n",atoi(msg_len),p1);
}
void Http_Dataanalysis(unsigned char *msg,unsigned int size)
{
    rt_kprintf("http msg len:%d msg:\n",size,msg);
}
